package org.test.bookpub.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.test.bookpub.entity.*;
import org.test.bookpub.repository.*;

import javax.annotation.PostConstruct;

@Service
//@Profile("production")//指定在哪个application.*.properies文件启动时生效
public class DatabaseLoader {
    @Autowired
    private TeammateRepository teammateRepository;
    @Autowired
    private TeamRepository teamRepository;
    @Autowired
    private BookRepository bookRepository;
    @Autowired
    private AuthorRepository authorRepository;
    @Autowired
    private PublisherRepository publisherRepository;

    public DatabaseLoader() {
    }

  /*  @Autowired
    public DatabaseLoader(TeammateRepository
                                  teammateRepository,
                          TeamRepository teamRepository) {
        this.teammateRepository = teammateRepository;
        this.teamRepository = teamRepository;
    }*/

    @PostConstruct//bean加载完成后，会自动运行一次PostConstruct方法
    private void initDatabase() {
        Team springBootTeam = new Team("Spring Boot Badgers");
        teamRepository.save(springBootTeam);
        Teammate greg = new Teammate("Greg", "Turnquist");
        greg.setPosition("2nd base");
        greg.setTeam(springBootTeam);
        teammateRepository.save(greg);
        Teammate roy = new Teammate("Roy", "Clarkson");
        roy.setPosition("1st base");
        roy.setTeam(springBootTeam);
        teammateRepository.save(roy);
        Teammate phil = new Teammate("Phil", "Webb");
        phil.setPosition("pitcher");
        phil.setTeam(springBootTeam);
        teammateRepository.save(phil);



        Author author = new Author("Alex", "Antonov");
        author = authorRepository.save(author);
        Publisher publisher = new Publisher("Packt");
        publisher = publisherRepository.save(publisher);
        Book book = new Book("978-1-78528-415-1", "Spring Boot Recipes",
                author, publisher);
        bookRepository.save(book);
    }
}